import { GSelect } from 'geminis-react';
import { forwardRef, useEffect, useRef, useState } from 'react';

export const PurchaseProductBatchSelect = forwardRef((props: any, ref: any) => {
  const [productValue, setProductValue] = useState<any>();
  const [batchSelectUrl, setBatchSelectUrl] = useState<any>();
  const productRef = useRef<any>();
  const batchRef = useRef<any>();

  const onProductChange = (value: any) => {
    setProductValue(value);
    setBatchSelectUrl(
      '/api/warehouse/purchase/detail/' + props.orderId + '/' + value + '/batch',
    );
  };

  useEffect(() => {
    batchRef.current.loadData();
  }, [batchSelectUrl]);

  useEffect(() => {
    if (props?.value) {
      // request('/api/warehouse/basic/product/batch', {
      //   method: 'post',
      //   data: {
      //     filter: {
      //       filters: [{ id: 'id', compareType: '=', value: props.value }],
      //     },
      //   },
      // })
      //   .then((resp: any) => {
      //     const data = resp.content[0];
      //     onProductChange(data.product.id);
      //   })
      //   .catch((err: any) => {
      //     message.error('获取数据错误');
      //     throw err;
      //   });
    }
  }, [props?.value]);

  return (
    <>
      <GSelect
        style={{ width: '50%' }}
        ref={productRef}
        url={'/api/warehouse/purchase/detail/' + props.orderId + '/product'}
        showSearch
        onChange={onProductChange}
        value={productValue}
      />
      <GSelect
        style={{ width: '50%' }}
        ref={batchRef}
        url={batchSelectUrl}
        showSearch
        fieldNames={{
          label: 'no',
          value: 'id',
        }}
        onChange={props?.onChange}
        value={props?.value}
      />
    </>
  );
});
